// 随附单证相关操作mixins
import countryOForiModal from '../../components/countryOForiModal'
/**
 * 随附单证相关mixins  保存、新增、删除、原产地
 */

export default {
    components: {
        countryOForiModal
    },
    data () {
        return {
            declareEdocList: [],
            modalParams: {
                countryOForiShow: false,
                countryOForiParams: {}
            }
        }
    },
    methods: {
        initEdocList (data) {
            data.forEach((item, i) => {
                item._edocIndex = i + 1
            })
            this.declareEdocList = data
        },
        // 保存一条随附单证
        handleSaveEdoc (data) {
            const { docuCode, certCode } = data
            if (['Y', 'E', 'R', 'F', 'J'].includes(docuCode)) {
                if (data.customsDeclareEdocRealas) {
                    data.customsDeclareEdocRealas.forEach(item => {
                        item.ecoCertNo = certCode
                        item.certType = docuCode
                    })
                }
            } else {
                data.customsDeclareEdocRealas = []
            }
            const index = this._edocIndex // 随附单证自定义序号
            if (!index) {
                // 新增保存
                data._edocIndex = this.declareEdocList.length + 1
                this.declareEdocList.push(data)
            } else {
                // 编辑保存
                data._edocIndex = index // 获取自定义序号
                this.declareEdocList.splice(index - 1, 1, data)
            }
            this.setAttachedEdoc()
            this.clearEdocErrById(data.declareDocId, this.getFormTips('edocForm'))
            this.refrehNumOfDoc()
        },
        // 删除随附单证
        handleDeleteEdoc () {
            const id = this._edocIndex
            if (id) {
                this.$Modal.confirm({
                    title: '删除单证信息',
                    content: '删除单证信息,数据无法恢复,是否确认删除？',
                    onOk: () => {
                        this.declareEdocList.splice(id - 1, 1)
                        this.setAttachedEdoc()
                        this.declareEdocList.forEach((item, index) => {
                            item._edocIndex = index + 1
                        })
                        this.$Message.success('单证信息删除成功！')
                        const declareDocId = this.getFormByKey('edocForm', 'declareDocId')
                        this.clearEdocErrById(declareDocId)
                        this.refrehNumOfDoc()
                    }
                })
            } else {
                this.$Message.warning('请勾选需要删除的单证信息')
            }
        },

        // 新建随附单证
        refrehNumOfDoc () {
            this._edocIndex = '' // 清空序号
            this.resetForm('edocForm')
        },
        // 单击随附单证表格
        getNumDetails (row) {
            this._edocIndex = row._edocIndex // 设置序号
            this.setFormData('edocForm', row)
            this.$nextTick(() => {
                // 清空随附单证表单校验信息
                this.clearFormTips('edocForm')
                const id = row.declareDocId
                if (id && this.eDocErrDataList[id]) {
                    const errs = this.eDocErrDataList[id]
                    this.setFormTips('edocForm', errs)
                }
            })
        },

        /**
         * 删除随附单证审核校验信息by 随附单证id
         * @param {String} id 商品id
         * @param {Object} errors 本地校验信息
         */
        clearEdocErrById (id, errors = null) {
            if (id && this.eDocErrDataList && this.eDocErrDataList[id]) {
                let noErr = true
                if (errors) {
                    const keys = Object.keys(this.eDocErrDataList[id])
                    keys.forEach(k => {
                        if (errors[`${k}Msg`]) {
                            noErr = false
                        } else {
                            delete this.eDocErrDataList[id][k]
                        }
                    })
                }
                if (noErr) {
                    this.eDocErrDataList[id] = null
                }
            }
        },

        setAttachedEdoc () {
            const value = this.declareEdocList.length
                ? this.declareEdocList.map(item => item.docuCode).join(',')
                : ''
            this.setFormData('declareForm', 'attachedEdoc', value)
        },
        // 打开原产地弹窗
        openCountryModal () {
            if (!this.declareEdocList.length || !this._edocIndex) {
                this.$Message.warning('请选择单证编号或保存单证编号')
                return false
            }

            if (this.CommInfoDataList.length === 0) {
                this.$Message.warning('商品信息为空！')
                return false
            }

            let arr = ['Y', 'E', 'R', 'F', 'J']
            let containerDate = this.getFormByKey('edocForm')
            containerDate.numid = this._edocIndex
            if (arr.includes(containerDate.docuCode)) {
                this.modalParams.countryOForiParams = {
                    CustomsDeclareContainerDate: containerDate,
                    CommInfoDataList: this.CommInfoDataList
                }
                this.modalParams.countryOForiShow = true
            } else {
                this.$Message.warning('单证代码必须为Y/E/R/F/J!')
            }
        },
        // 保存原产地对应关系数据
        commitCountryOFori (res) {
            const index = this.declareEdocList.findIndex(item => item._edocIndex == res.parentCurrentId)
            if (index > -1) {
                const {
                    docuCode,
                    certCode
                } = this.declareEdocList[index]
                res.list.forEach(item => {
                    item.ecoCertNo = certCode
                    item.certType = docuCode
                })
                this.declareEdocList[index].customsDeclareEdocRealas = JSON.parse(JSON.stringify(res.list))
                this.setFormData('edocForm', 'customsDeclareEdocRealas', res.list.slice())
            }
        }
    }
}
